﻿using Domain;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infratructure.RepositoryWay
{
    public class Repository<T> : IRepository<T> where T : class, new()
    {
        private readonly ISqlSugarClient sqlSugar;

        public Repository(ISqlSugarClient sqlSugar)
        {
            this.sqlSugar = sqlSugar;
        }
        /// <summary>
        /// 查询某条数据
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>数据</returns>
        public async Task<T?> GetByIdAsync(long id)
        {
            return await sqlSugar.Queryable<T>().InSingleAsync(id); //根据主键查询某条具体数据
        }
        /// <summary>
        /// 添加某条数据
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>影响行数</returns>

        public async Task<int> AddAsync(T t)
        {
            return await sqlSugar.Insertable(t).ExecuteCommandAsync();  //参数需要有无参构造函数
        }
        /// <summary>
        /// 修改某条数据
        /// </summary>
        /// <param name="t">修改后的数据</param>
        /// <returns>影响行数</returns>
        public async Task<int> UpdateAsync(T t)
        {
            return await sqlSugar.Updateable(t).ExecuteCommandAsync(); //对某条数据进行修改
        }
        /// <summary>
        /// 删除某条数据 真删
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>影响行数</returns>
        public async Task<int> DeleteAsync(long id)
        {
            return await sqlSugar.Deleteable<T>().In(id).ExecuteCommandAsync();  //根据主键删除某条数据
        }
        /// <summary>
        /// 获取可延迟查询的对象（不立即执行）
        /// </summary>
        public ISugarQueryable<T> GetAll()
        {
            return sqlSugar.Queryable<T>();
        }


    }
}
